package org.example.hzit.controller;

import org.example.hzit.UserVo.UserVo;
import org.example.hzit.entity.User;
import org.example.hzit.service.IUserService;
import org.example.hzit.util.PageResult;
import org.example.hzit.util.R;
import org.example.hzit.util.R1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author gww
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:8082")
//@RefreshScope
public class UserController {

    @Autowired
    private IUserService userService;


    @Value("${student.sname}")
    private String sname;

    @Value("${student.age}")
    private int age;

    @Value("${classes.cname}")
    private String cname;




    @GetMapping("/list")
    public R<List<User>> getuser(){
        return R.ok(userService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize){
        PageResult<User> w = userService.findByPage(page,pageSize);
        return R.ok(w);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,@PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) UserVo vo){
        PageResult<User>w=userService.search(page,pageSize,vo);
        return R.ok(w);
    }

    @PostMapping("/user")
    public User adduser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @DeleteMapping("/delete/{ids}")
    public R1 delete(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return userService.removeByIds(sids) ? R1.ok().message("删除学生成功!") : R1.error().message("删除学生失败!");
    }

}
